/*
	FileName : JT_C_TIME.c
	Description :
		Manage the JT_C time opperations.
	
	Created on 2017.11.27 by WFL.

	Revision History:
		

*/


/* Includes ------------------------------------------------------------------*/
#include "JT_C_TIME.h"

//-------------------------------------
//---------------------------------------------------------------------
// JTC_TIME format: b 0 - b 5: second
//					b 6 - b11: minute
//					b12 - b16: hour
//					b17 - b21: date
//					b22 - b25: month
//					b26 - b31: year(offset 2000)
uint32_t JT_C_TIME = 0;		//AppInfo.NowTime
//---------------------------------------------------------------------

uint8_t JTC_TIME_getSeco(uint32_t jtc_t)
{
	return ((jtc_t >> 0) & 0x3F);	//6 bit
}
uint8_t JTC_TIME_getMinu(uint32_t jtc_t)
{
	return ((jtc_t >> 6) & 0x3F);	//6 bit
}
uint8_t JTC_TIME_getHour(uint32_t jtc_t)
{
	return ((jtc_t >> 12) & 0x1F);	//5 bit
}
uint8_t JTC_TIME_getDate(uint32_t jtc_t)
{
	return ((jtc_t >> 17) & 0x1F);	//5 bit
}
uint8_t JTC_TIME_getMont(uint32_t jtc_t)
{
	return ((jtc_t >> 22) & 0x0F);	//4 bit
}
uint8_t JTC_TIME_getYear(uint32_t jtc_t)
{
	return ((jtc_t >> 26) & 0x3F);	//6 bit
}
//----------------------------------
void JTC_TIME_setSeco(uint32_t *jtc_t,uint32_t sec)
{
	*jtc_t |= ((sec & 0x3F)<<0);
	return;
}
void JTC_TIME_setMinu(uint32_t *jtc_t,uint32_t min)
{
	*jtc_t |= ((min & 0x3F)<<6);
	return;
}
void JTC_TIME_setHour(uint32_t *jtc_t,uint32_t hour)
{
	*jtc_t |= ((hour & 0x1F)<<12);
	return;
}
void JTC_TIME_setDate(uint32_t *jtc_t,uint32_t date)
{
	*jtc_t |= ((date & 0x1F)<<17);
	return;
}
void JTC_TIME_setMont(uint32_t *jtc_t,uint32_t mon)
{
	*jtc_t |= ((mon & 0x0F)<<22);
	return;
}
void JTC_TIME_setYear(uint32_t *jtc_t,uint32_t year)
{
	*jtc_t |= ((year & 0x3F)<<26);
	return;
}
uint32_t JTC_TIME_makeUpTime(uint8_t y,
								uint8_t mo,
								uint8_t d,
								uint8_t h,
								uint8_t mi,
								uint8_t s)
{
	uint32_t newtime;
	
	newtime = 0;
	JTC_TIME_setSeco(&newtime,s);
	JTC_TIME_setMinu(&newtime,mi);
	JTC_TIME_setHour(&newtime,h);
	JTC_TIME_setDate(&newtime,d);
	JTC_TIME_setMont(&newtime,mo);
	JTC_TIME_setYear(&newtime,y);
	return newtime;
}
//-----------------------------------------------------------
#define ISCOMMANYEAR(year)  	(((year%4)!=0)||((year%100)==0 && (year%400)!=0))

uint8_t JTC_TIME_timeCheckErr(uint32_t time)
{
	uint8_t Sec,Min,Hour,Date,Month,Year;
	
	Sec   = JTC_TIME_getSeco(time);
	Min   = JTC_TIME_getMinu(time);
	Hour  = JTC_TIME_getHour(time);
	Date  = JTC_TIME_getDate(time);
	Month = JTC_TIME_getMont(time);
	Year  = JTC_TIME_getYear(time);
	
	if(Sec >= 60)
		return 1;
	
	if(Min >= 60)
		return 1;
	
	if(Hour >= 24)
		return 1;
	
	if(JTC_TIME_dateCheckErr(Year,Month,Date))
		return 1;
	
	if(Month > 12 || Month < 1)
		return 1;
	
	if(Year >= 100)
		return 1;
	
	return 0;
}
uint8_t JTC_TIME_dateCheckErr(uint8_t year,uint8_t mon, uint8_t date)
{
	if(
		(date>31&&mon==1)||
		(date>28&&mon==2&&ISCOMMANYEAR(year))||   //??
		(date>29&&mon==2)||   //??
		(date>31&&mon==3)||
		(date>30&&mon==4)||
		(date>31&&mon==5)||
		(date>30&&mon==6)||
		(date>31&&mon==7)||
		(date>31&&mon==8)||
		(date>30&&mon==9)||
		(date>31&&mon==10)||
		(date>30&&mon==11)||
		(date>31&&mon==12)
		)
		return 1;
	
	if(date < 1)
		return 1;
	
	return 0;
}
/*
 * return time added "time" by "period"
 */
uint32_t JTC_TIME_timeAddSec(uint32_t time,uint16_t priod)
{
	uint32_t Sec;
	uint8_t Min,Hour,Date,Month,Year;
	
	Sec   = JTC_TIME_getSeco(time);
	Min   = JTC_TIME_getMinu(time);
	Hour  = JTC_TIME_getHour(time);
	Date  = JTC_TIME_getDate(time);
	Month = JTC_TIME_getMont(time);
	Year  = JTC_TIME_getYear(time);
	
	Sec += priod;
	
	while(Sec>=60)
	{
		Sec -= 60;
		Min++;
		if(Min >= 60)
		{
			Min = 0;
			Hour++;
			if(Hour >= 24)
			{
				Hour = 0;
				Date++;
				if(JTC_TIME_dateCheckErr(Year,Month,Date))
				{
					Date=1;
					Month++;
					if(Month>=13)
					{
						Month=1;
						Year++;
					}
				}
			}
		}
	}
	
	return JTC_TIME_makeUpTime(Year,Month,Date,Hour,Min,Sec);
}
//-----------------------------------------------

//------------------

//-----------------------------------------------------------------------






///EOF
